<?php

if (!defined('DIR_SEP')) {
    define('DIR_SEP', DIRECTORY_SEPARATOR);
}

if (!defined('MODULES_DIR')) {
    define('MODULES_DIR', dirname(__FILE__).DIR_SEP."..".DIR_SEP);
}
require_once(MODULES_DIR.'db.inc');
require_once(MODULES_DIR.'util.inc');
require_once(MODULES_DIR.'tag.inc');

class Feed
{
	protected $db;
	public $id;
	public $feedName;
	public $description;
	public $url;
	public $lastPost;
	
	function __construct()
	{
		$this->db = DB::getInstance()->getDB();
	}
	
	public function load($feedId)
	{
		try
		{
			$sql ="select id, feed_name, description, url
					FROM t_feed 
					WHERE id = ".$feedId;

			$query = mysql_query($sql,$this->db);

			if (!$query)
				throw new Exception(mysql_error());

			$row = mysql_fetch_row($query);

			$this->id = $feedId;
			$this->feedName = $row[1];
			$this->description = $row[2];
			$this->url = $row[3];

			return true;
		}
		catch(Exception $ex)
		{
			die($ex->getMessage());
			return false;
		}
	}
		
	function update()
	{
		try
		{
			$sql ="update t_feed set
					feed_name = '".$this->feedName."',
					description = '".$this->description."',
					url = '".$this->url."' 
					WHERE id = ".$this->id;

			$query = mysql_query($sql,$this->db);

			if (!$query)
				throw new Exception(mysql_error());

			return true;
		}
		catch(Exception $ex)
		{
			die($ex->getMessage());
			return false;
		}
	}
	
	function addTag($feedId, $tagId)
	{
		$query = mysql_query("insert into t_feed_tag_item
								values( ".$feedId.",".$tagId.")"
							,$this->db);
	}

	function deleteTag($feedId,$tagId)
	{
		$query = mysql_query("delete from t_feed_tag_item
							where feed_id =".$feedId
							." and tag_id =".$tagId
						,$this->db);
	}	
	
	function add($name,$description,$url)
	{
		try
		{
			$query = mysql_query("insert into t_feed (feed_name,description,url)
									values ('".$name."','".$description."','".$url."')"
									,$this->db);
			if(!$query)
				throw new Exception("cannot delete feed");
				
			return 1;
		}
		catch(Exception $ex)
		{
			return 0;
		}
		
	}
	
	function delete($feedId)
	{
		try
		{
			mysql_query('BEGIN',$this->db);
			
			$query = mysql_query("delete from t_feed_tag_item
									where feed_id =".$feedId
									,$this->db);
			if(!$query)
				throw new Exception("cannot delete feed");

			$query = mysql_query("update t_feed set deleted = 1 where id = ".$feedId,$this->db);
			
			if(!$query)
				throw new Exception("cannot delete feed");
			
			mysql_query('COMMIT',$this->db);
		}
		catch(Exception $ex)
		{
			mysql_query('ROLLBACK',$this->db);			
		}
	}	
	
	static function allFeeds($withDeleted)
	{
		try
		{
			$sql ="select id, feed_name, description, url,last_post from t_feed tf
					LEFT OUTER JOIN ( SELECT max( entry_date ) as last_post, feed_id
										FROM t_feed_item
										GROUP BY feed_id) tfi 
					ON tfi.feed_id = tf.id";

			$sql.= !$withDeleted ? " where deleted = 0" : "";
			
			$query = mysql_query($sql,DB::getInstance()->getDB());

			if (!$query)
				throw new Exception(mysql_error());
			
			$result = array();
			
			while( list($id,$name,$description,$url, $lastPost) = mysql_fetch_row($query))
			{
				$feed = new Feed(DB::getInstance()->getDB());
				
				$feed->id = $id;
				$feed->feedName = $name;
				$feed->description = $description;
				$feed->url = $url;
				$feed->lastPost = strtotime($lastPost);
				$result[] = $feed;
			}
			
			return $result;
		}
		catch(Exception $ex)
		{
			return null;
		}
	}
	
	static function getTagCloud($feedId)
	{
		return DB::getInstance()->query(
			"select tg.tag, tg.id, tgi.tag_id
			from 	t_tag tg
			left outer join t_feed_tag_item tgi
			on 		( tg.id = tgi.tag_id and tgi.feed_id = ".$feedId.")
			order by tg.tag" );
	}	
}

?>